package com.corticon.dialogs.util;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.corticon.dialogs.dao.GenericDAO;
import com.corticon.dialogs.model.PermissionType;
import com.corticon.dialogs.util.security.CustomUserDetails;
import com.corticon.dialogs.util.security.SecurityUtils;

/**
 * This class contains some methods to support the lookup functions.
 *
 * @author Lifan Yang
 * @since 2008-10-15
 */
@Component
public class LookupSupport {

    @Resource
    private BeanSupport beanSupport;

    public <T> List<T> getEntityList(Class<T> entityClass, CustomUserDetails currentAdmin) {

        List<T> entityList = new ArrayList<T>();
        List<String> permissions = SecurityUtils.getCurrentUserPermissions();

        GenericDAO dao = beanSupport.loadDAO(entityClass);

        if (permissions.contains(PermissionType.SYS_ADMIN.value())) {
            entityList = dao.findAll();
        } else if (permissions.contains(PermissionType.GROUP_ADMIN.value())) {
            T entity = (T) dao.findById(currentAdmin.getGroupName());
            entityList.add(entity);

            T entityAll = (T) dao.findById("ALL");
            entityList.add(entityAll);
        }

        return entityList;
    }
}
